/**
 * 
 */
package spatial.geometry;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Here we collect some Utility functions for Point2D.
 * This util sorts points firstly on x and then on y. 
 * 
 * @author wu wei
 * 
 */
public class Point2DUtils {

	public static List<Point2D> xySort(Collection<Point2D.Double> ps) {
		List<Point2D> plist = new ArrayList<Point2D>(ps);
		Collections.sort(plist, new XYComparator());
		return plist;
	}

	public static class XYComparator implements Comparator<Point2D> {
		public int compare(Point2D o1, Point2D o2) {
			if (o1.getX() < o2.getX()) {
				return -1;
			} else if (o1.getX() > o2.getX()) {
				return 1;
			} else {
				if (o1.getY() < o2.getY()) {
					return -1;
				} else if (o1.getY() > o2.getY()) {
					return 1;
				} else {
					return 0;
				}
			}
		}
	}

}
